<!--
 * @author hemengxia
 * @desc 红包活动列表---部门设置
-->
<template>
  <section class="department-index">
    <el-dialog
      v-el-drag-dialog
      :visible.sync="visible"
      :close-on-click-modal="false"
      title="参与部门"
      center
      fullscreen
      @close="handleCancel()"
    >
      <section class="join-main">
        <el-form ref="form" :inline="true" :model="form" :rules="rules">

          <el-form-item label="选择部门" prop="deptIds">
            <el-cascader
              ref="org"
              v-model="form.deptIds"
              style="width:500px"
              collapse-tags
              clearable
              :props="orgProps"
              :options="orgTreeData"
              @change="handleChange"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSubmit">添加</el-button>
          </el-form-item>
        </el-form>
        <section class="main-table">
          <el-table :data="list" style="width: 100%">
            <el-table-column type="index" width="50" />
            <el-table-column prop="deptName" show-overflow-tooltip align="center" label="部门名称" />
            <el-table-column
              label="操作"
              fixed="right"
              align="center"
              min-width="100"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button type="text" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </section>
        <section class="main-page">
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="listQuery.page"
            :limit.sync="listQuery.limit"
            @pagination="getList"
          />
        </section>
      </section>
      <div slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="handleCancel()">关 闭</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import Pagination from '@/components/Pagination'
import elDragDialog from '@/directive/el-drag-dialog'
import { recordQueryPage, insert, getComAndOrgTree, del } from './api'
import { mapGetters } from 'vuex'

export default {
  name: 'Department',
  components: { Pagination },
  directives: { elDragDialog },
  props: {
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      rechList: [],
      visible: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      form: {
        deptIds: ''
      },
      rules: {
        deptIds: [{ required: true, message: '请选择部门', trigger: 'change' }]
      },
      list: [],
      dict: {
        QUESTIONLEVEL: [], // 试题难度
        STLX: [] // 试题类型
      },
      orgProps: {
        label: 'name',
        value: 'id',
        children: 'list',
        multiple: true,
        emitPath: false,
        checkStrictly: true
      },
      orgTreeData: []
    }
  },

  computed: {
    ...mapGetters([
      'user'
    ])
  },
  created() {
    this.visible = true
    this.getOrgTree()
    this.getList()
  },
  methods: {
    handleDelete(ids) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          del({ id: ids }).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch(() => {
      })
    },
    handleChange(value) {
      console.log(111, value)
      console.log(222, this.form.deptIds)
    },
    getOrgTree() {
      return new Promise((resolve, reject) => {
        getComAndOrgTree({ parentId: -1, orgId: this.user.orgId }).then(res => {
          this.orgTreeData = res.data.data
        }).catch(() => {
          resolve([])
        })
      })
    },
    getList(obj) {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      const params = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        redpacketsActivityId: this.id,
        referType: 'DEPT'
      }
      return new Promise((resolve, reject) => {
        recordQueryPage(params).then(({ data }) => {
          loading.close()
          this.list = data.data.rows
          this.total = data.data.total
        }).catch(error => {
          reject(error)
          loading.close()
        })
      })
    },

    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          const params = {
            redpacketsActivityId: this.id,
            deptIds: this.form.deptIds.join(',')
          }
          return new Promise((resolve, reject) => {
            insert(params).then(() => {
              loading.close()
              this.$message({
                title: '成功',
                message: '设置成功',
                type: 'success',
                duration: 2000
              })
              for (const i in this.form) {
                if (this.form['' + i + ''] instanceof Array) {
                  this.form['' + i + ''] = []
                } else {
                  this.form['' + i + ''] = ''
                }
              }
              this.getList()
            }).catch(error => {
              loading.close()
              reject(error)
            })
          })
        } else {
          return false
        }
      })
    },

    handleCancel() {
      this.$emit('cancel')
    }
  }
}
</script>
<style lang="scss">
.department-index {
  .join-main {
    min-height: calc(100vh - 140px);
    max-height: calc(100vh - 140px);
    overflow: hidden;
    position: relative;

    .main-table {
      position: absolute;
      top: 70px;
      left: 0;
      right: 0;
      bottom: 72px;
      box-sizing: border-box;
      overflow: hidden;

      &::-webkit-scrollbar {
        width: 8px; /*对垂直流动条有效*/
        height: 1px; /*对水平流动条有效*/
      }

      /** 滚动条轨道*/
      &::-webkit-scrollbar-track {
        box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
        background-color: white;
        border-radius: 3px;
      }

      /** 滚动条里面小方块*/
      &::-webkit-scrollbar-thumb {
        height: 32px;
        border-radius: 5px;
        box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.1);
        background-color: #eaeaea;
      }

      &:hover {
        overflow: auto;
      }
    }

    .main-page {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
    }
  }
}
</style>
